package com.sun.javaalgorithm.day04;

/**
 * @version 1.0
 * @Package: com.sun.javaalgorithm.day04
 * @ClassName: PascalTriangle
 * @Author: sgq
 * @Date: 2024/4/17 11:32
 * @Description: 杨辉三角
 */
public class PascalTriangle {

    /**
     * 获取杨辉三角指定位置的数字
     * @param i
     * @param j
     * @return
     */
    public static int element(int i, int j) {
        if (j == 0 || i == j) {
            return 1;
        }
        return element(i - 1, j - 1) + element(i - 1, j);
    }

    /**
     * 打印n行杨辉三角
     * @param n
     */
    public static void print(int n) {
        for (int i = 0; i < n; i++) {
            if (i < n - 1) {
                System.out.printf("%" + 2 * (n - 1 - i) + "s", " ");
            }

            for (int j = 0; j < i + 1; j++) {
                System.out.printf("%-4d", element(i, j));
            }
            System.out.println();
        }
    }


    public static void main(String[] args) {
        System.out.println(element(4, 2));
        print(6);
    }

}
